<?php
	error_reporting(E_ALL);
	set_time_limit(0);
	date_default_timezone_set('Asia/Shanghai');
	include __DIR__.'/../src/Curl.php';
	include __DIR__.'/../src/CRedis.php';
	
    class collect{
    
    	const RATE_API = 'http://api.jijinhao.com/realtime/quotejs.htm?categoryId=204&currentPage=1&pageSize=7';
    	const METAL_API = 'http://api.jijinhao.com/realtime/quotejs.htm?codes=JO_38496%2CJO_38493&currentPage=1&pageSize=2';
    	
    	const CACHE_PREFIX = 'fx_metal_';
    	
    	private $_curl = null;
    	
    	public $interval = 10;
    
        public function __construct() {
        	$this->_curl = Curl::model();
        	$this->_curl->referer = 'http://www.cngold.org/img_date/livesilvercn_rmb.html';
            
        }
        
        public function run() {
        	$redis = new CRedis;
        
        	$testTime = 0;
        	$lastLogTime = 0;
        	
        	$lastDay = '';
        	$fp = null;
        	
        	while(1) {
        		$day = date('Y-m-d');
        		$time = time();
        		$sleep = 8;
        		
        		if($day != $lastDay) {
        			$fp && fclose($fp);
        			$file = __DIR__.'/data/'.$day.'.txt';
        			$fp = fopen($file, 'a+');
        			$lastDay = $day;
        		}
        		
        		$rate = $this->getRate();
        		if(!$rate) {
        			sleep(2);
        			continue;
        		}
        		$price = $this->getPrice();
        		if(!$price) {
        			sleep(2);
        			continue;
        		}
        		
        		$silver = $price[0];
        		$glod = $price[1];
        		//list($silver, $glod) = $price;
        		
        		$mod = $time % $this->interval;
        		$logTime = date('H:i:s', $time - $mod);
        		$sleep = $this->interval - $mod;
        		
        		$contents = sprintf('%s|%d|%.04f|%.02f|%.03f|%.02f|%.03f',
        			$logTime, $time-$mod, $rate, $silver, $rate*$silver/31.1, $glod, $rate*$glod/31.1);
        			
        		$redis->set(self::CACHE_PREFIX.$logTime, $contents);

        		fwrite($fp, $contents."\n");
        			
        		printf("success, %s \n", date('Y-m-d H:i:s', $time));
        		sleep($sleep);
        	}
        }
        
        /**
         * 获取美元对人民币的汇率
         */
        public function getRate() {
        	$contents = $this->openApi(self::RATE_API);
        	if(!$contents) return null;
        	
        	return $contents->data[0]->quote->q63;
        }
        
        /**
         * 获取国际黄金，白银价格
         */
        public function getPrice() {
        	$contents = $this->openApi(self::METAL_API);
        	if(!$contents) return null;
        	
        	$silver = $contents->data[0]->quote;
        	$glod = $contents->data[1]->quote;
        	return array($silver->q2 + $silver->q70, $glod->q2 + $glod->q70);
        }
        
        /**
         * 请求api内容，并解析
         */
        public function openApi($api) {
        	$contents = $this->_curl->request($api.'&_='.time().'000');
        		
        	$contents = preg_replace("#^var\s+quot_str\s+\=\s+\[#", "", $contents);
        	$contents = substr($contents, 0, -1);
        		
        	return @ json_decode($contents);
        }
        
        public function openLogFile() {
        	
        }
        
    }

    $c = new collect;
    $c->run();